package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.MallMemberEquityTemplateDao;
import com.sc.nft.entity.MallMemberEquity;
import com.sc.nft.entity.MallMemberEquityTemplate;
import com.sc.nft.entity.dto.AdminMemberEquityTemplateDetailDTO;
import com.sc.nft.entity.dto.AdminMemberEquityTemplatePageDTO;
import com.sc.nft.entity.dto.MallMemberEquityTemplateDetailDTO;
import com.sc.nft.entity.vo.EditMemberEquityTemplateVO;
import com.sc.nft.entity.vo.MallMemberEquityTemplatePageVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.MallMemberEquityService;
import com.sc.nft.service.MallMemberEquityTemplateService;
import com.sc.nft.util.Beans;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商城会员权益模版 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-29
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MallMemberEquityTemplateServiceImpl implements MallMemberEquityTemplateService {

    private final MallMemberEquityTemplateDao mallMemberEquityTemplateDao;
    @Autowired
    MallMemberEquityService mallMemberEquityService;

    /**
     * 获取商城会员权益模版列表(管理后台)
     *
     * @param mallMemberEquityTemplatePageVO
     * @return
     */
    @Override
    public Page<AdminMemberEquityTemplatePageDTO> getAdminMemberEquityTemplatePageDTO(MallMemberEquityTemplatePageVO mallMemberEquityTemplatePageVO) {
        return mallMemberEquityTemplateDao.getAdminMemberEquityTemplatePageDTO(mallMemberEquityTemplatePageVO.getPageNo(), mallMemberEquityTemplatePageVO.getPageSize(), mallMemberEquityTemplatePageVO.getStatus());
    }

    /**
     * 获取商城会员权益模版详情
     *
     * @param id
     * @return
     */
    @Override
    public AdminMemberEquityTemplateDetailDTO getAdminMemberEquityTemplateDetailDTO(Long id) {
        MallMemberEquityTemplate mallMemberEquityTemplate = getById(id);
        Assert.notNull(mallMemberEquityTemplate, () -> new GlobalRunTimeException("商城会员权益模版不存在"));
        return AdminMemberEquityTemplateDetailDTO.build(mallMemberEquityTemplate);
    }

    /**
     * 添加或者编辑商城会员权益模版
     *
     * @param editMemberEquityTemplateVO
     */
    @Override
    public void edit(EditMemberEquityTemplateVO editMemberEquityTemplateVO) {
        if (Objects.isNull(editMemberEquityTemplateVO.getId())) {
            // 添加
            MallMemberEquityTemplate mallMemberEquityTemplate = new MallMemberEquityTemplate();
            Beans.copyProperties(editMemberEquityTemplateVO, mallMemberEquityTemplate);
            mallMemberEquityTemplate.setStatus(Boolean.FALSE);
            mallMemberEquityTemplateDao.save(mallMemberEquityTemplate);
        } else {
            // 编辑
            MallMemberEquityTemplate mallMemberEquityTemplate = getById(editMemberEquityTemplateVO.getId());
            Assert.notNull(mallMemberEquityTemplate, () -> new GlobalRunTimeException("商城会员权益模版不存在"));
            Assert.isFalse(mallMemberEquityTemplate.getStatus(), () -> new GlobalRunTimeException("商城会员权益模版已启用，不可编辑"));
            List<MallMemberEquity> mallMemberEquityList = mallMemberEquityService.getByMallMemberEquityTemplateId(editMemberEquityTemplateVO.getId());
            Assert.isTrue(mallMemberEquityList.isEmpty(), () -> new GlobalRunTimeException("商城会员权益模版已被使用，不可编辑"));
            Beans.copyPropertiesIgnoreNull(editMemberEquityTemplateVO, mallMemberEquityTemplate);
            mallMemberEquityTemplateDao.updateById(mallMemberEquityTemplate);
        }
    }

    /**
     * 商城会员权益模版启用
     *
     * @param id
     */
    @Override
    public void enable(Long id) {
        MallMemberEquityTemplate mallMemberEquityTemplate = getById(id);
        Assert.notNull(mallMemberEquityTemplate, () -> new GlobalRunTimeException("商城会员权益模版不存在"));
        mallMemberEquityTemplateDao.updateStatus(id, Boolean.TRUE);
    }

    /**
     * 商城会员权益模版禁用
     *
     * @param id
     */
    @Override
    public void disable(Long id) {
        MallMemberEquityTemplate mallMemberEquityTemplate = getById(id);
        Assert.notNull(mallMemberEquityTemplate, () -> new GlobalRunTimeException("商城会员权益模版不存在"));
        List<MallMemberEquity> mallMemberEquityList = mallMemberEquityService.getByMallMemberEquityTemplateId(id);
        Assert.isTrue(mallMemberEquityList.isEmpty(), () -> new GlobalRunTimeException("商城会员权益模版已被使用，不可禁用"));
        mallMemberEquityTemplateDao.updateStatus(id, Boolean.FALSE);
    }

    /**
     * 通过id获取
     *
     * @param id
     * @return
     */
    @Override
    public MallMemberEquityTemplate getById(Long id) {
        return mallMemberEquityTemplateDao.getById(id);
    }

    /**
     * 通过idList获取
     *
     * @param idList
     * @return
     */
    @Override
    public List<MallMemberEquityTemplate> getByIdList(List<Long> idList) {
        return mallMemberEquityTemplateDao.getByIdList(idList);
    }

    /**
     * 获取所有已使用的权益模版
     *
     * @return
     */
    @Override
    public List<MallMemberEquityTemplate> templatesAlreadyUsed() {
        return mallMemberEquityTemplateDao.templatesAlreadyUsed();
    }

    /**
     * 获取商城会员权益模版详情
     *
     * @param id
     * @return
     */
    @Override
    public MallMemberEquityTemplateDetailDTO getAdminMallMemberTemplateDetailDTO(Long id) {
        MallMemberEquityTemplate mallMemberEquityTemplate = getById(id);
        Assert.notNull(mallMemberEquityTemplate, () -> new GlobalRunTimeException("商城会员权益模版不存在"));
        return MallMemberEquityTemplateDetailDTO.build(mallMemberEquityTemplate);
    }
}
